package com.basketball.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.basketball.gym.annotation.ApiJsonObject;
import com.basketball.gym.annotation.ApiJsonProperty;
import com.basketball.gym.pojo.Orders;
import com.basketball.gym.pojo.ResponseData;
import com.basketball.gym.pojo.User;
import com.basketball.gym.pojo.state.ResponseState;
import com.basketball.gym.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 赵肖云
 * @since 2021-03-27
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/gym/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("计算价格")
    @PostMapping("/calculatePrice")
    public ResponseData<Orders> calculatePrice(@Valid @RequestBody Orders order) {
        return orderService.calculatePrice(order);
    }

    @ApiOperation("发起订单")
    @PostMapping
    public ResponseData<Orders> postOrder(@Validated(Orders.post.class) @RequestBody Orders order, @ApiIgnore @ModelAttribute("activeUser") User user) {
        return orderService.postOrder(order, user);
    }

    @ApiOperation("充值")
    @PostMapping("/topUp")
    public ResponseData<Object> topUp(@Validated(Orders.topUp.class) @RequestBody Orders order, @ApiIgnore @ModelAttribute("activeUser") User user) {
        try {
            return orderService.topUp(order, user);
        } catch (ParseException e) {
            return new ResponseData<>(ResponseState.ERROR.getMessage(), ResponseState.ERROR.getValue());
        }
    }

    @ApiOperation("余额支付")
    @PostMapping("/pay/balance")
    public ResponseData<Orders> payByBalance(@Validated(Orders.pay.class) @RequestBody Orders order, @ApiIgnore @ModelAttribute("activeUser") User user) {
        return orderService.payByBalance(order, user);
    }

    @ApiOperation("微信支付")
    @PostMapping("/pay/wx")
    public ResponseData<Object> payByWx(@Validated(Orders.pay.class) @RequestBody Orders order, @ApiIgnore @ModelAttribute("activeUser") User user) {
        return orderService.payByWx(order, user);
    }

    @ApiOperation("支付结果回调")
    @PostMapping("/pay/result")
    public void payResult(@RequestBody Map<String, Object> param) {
        orderService.payResult(param);
    }

    @ApiOperation("查询个人订单(可通过id,type,state,date筛选）")
    @GetMapping
    public ResponseData<List<Orders>> getOrders(Orders order, @ApiIgnore @ModelAttribute("activeUser") User user) {
        return orderService.getOrders(order, user);
    }

    @ApiOperation("取消订单（只能取消包场的订单）")
    @PostMapping("/cancel")
    public ResponseData<Orders> cancelOrder(@ApiJsonObject(name = "CancelOrder", value = {
            @ApiJsonProperty(key = "id", description = "订单Id")
    }) @RequestBody Map<String, Object> param, @ApiIgnore @ModelAttribute("activeUser") User user) {
        try {
            Integer orderId = (Integer) param.get("id");
            return orderService.cancelOrder(orderId, user);
        } catch (ParseException e) {
            return new ResponseData<>(ResponseState.ERROR.getMessage(), ResponseState.ERROR.getValue());
        }
    }

//    @ApiOperation("退款回调")
//    @PostMapping("/refund/result")
//    public void refundResult(@RequestBody Map<String,Object> param){
//        orderService.refundResult(param);
//    }

    @ApiOperation("管理员查询订单(可通过id,type,state,date,user_id,user.phone筛选）")
    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @GetMapping("/admin")
    public ResponseData<IPage<Orders>> adminGetOrders(Orders order) {
        return orderService.adminGetOrders(order);
    }

    @ApiOperation("管理员设置完成订单")
    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @PostMapping("/admin/finish")
    public ResponseData<Orders> adminFinishOrder(@Validated(Orders.pay.class)@RequestBody Orders order) {
        return orderService.adminFinishOrder(order);
    }


}

